import {http} from './index'
// 登录
export const Login = (data) => http({
    url: "/users/login",
    method: "POST",
    data: data
})
// 注册
export const Register = (data) => http({
    url: "/users/register",
    method: "POST",
    data: data
})
// 商品列表
export const GoodList = (params) => http({
    url: "/goods",
    method: "GET",
    params: params
})

//加入购物车
export const addCart = (data) => {
    // 获取 token，假设您存储在 storage 中
    const token = uni.getStorageSync('token');
    return http({
        url: "/carts",
        method: "POST",
        data: data,
        header: {
            'Authorization': `Bearer ${token}`, // 添加 token 到请求头
            'Content-Type': 'application/json' // 确保请求类型
        }
    });
};

//获取购物车
export const getCart = (params) => {
    // 获取 token，假设您存储在 storage 中
    const token = uni.getStorageSync('token');
    return http({
        url: "/carts",
        method: "GET",
        params: params,
        header: {
            'Authorization': `Bearer ${token}`, // 添加 token 到请求头
            'Content-Type': 'application/json' // 确保请求类型
        }
    });
};

//更新购物车
export const updateCart = (id, data) => {
    // 获取 token，假设您存储在 storage 中
    const token = uni.getStorageSync('token');

    return http({
        url: `/carts/${id}`, // 将 ID 添加到 URL 中
        method: "PATCH",
        data: data,
        header: {
            'Authorization': `Bearer ${token}`, // 添加 token 到请求头
            'Content-Type': 'application/json' // 确保请求类型
        }
    });
};

//全选购物车
export const selectAllCart = () => {
    const token = uni.getStorageSync('token');
    return http({
        url: `/carts/selectAll`,
        method: "POST",
        header: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
        }
   });
}

//删除购物车
export const deleteCart = (data) => {
    const token = uni.getStorageSync('token');
    return http({
        url: `/carts`,
        method: "DELETE",
        data: data,
        header: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
        }
   });
}

// 修改密码
export const changePassword = (data) => {
    const token = uni.getStorageSync('token');
    return http({
        url: `/users`,
        method: "PATCH",
        data: data,
        header: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
        }
   });
}

// 添加地址
export const addAddress = (data) => {
    const token = uni.getStorageSync('token');
    return http({
        url: `/address`,
        method: "POST",
        data: data,
        header: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
        }
   });
}

// 查询地址
export const getAddress = () => {
    const token = uni.getStorageSync('token');
    return http({
        url: `/address`,
        method: "GET",
        header: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
        }
   });
}

// 设置默认地址
export const setDefaultAddress = (id) => {
    const token = uni.getStorageSync('token');
    return http({
        url: `/address/${id}`,
        method: "PATCH",
        header: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
        }
   });
}

// 删除地址
export const deleteAddress = (id) => {
    const token = uni.getStorageSync('token');
    return http({
        url: `/address/${id}`,
        method: "DELETE",
        header: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
        }
   });
}

// 创建订单
export const createOrder = (data) => {
    const token = uni.getStorageSync('token');
    return http({
        url: `/order`,
        method: "POST",
        data: data,
        header: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
        }
   });
}

// 查询订单
export const getOrder = (params) => {
    const token = uni.getStorageSync('token');
    return http({
        url: `/order`,
        method: "GET",
        params: params,
        header: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
        }
   });
}
// 推荐列表
export const recommend = (params) => {
    const token = uni.getStorageSync('token');
    return http({
        url: `/recommend`,
        method: "GET",
        params: params,
        header: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
        }
    })
}
// 添加评论
export const comment = (data) => {
    return http({
        url: `/comment`,
        method: "POST",
        data: data,
    });
}